﻿Public Class Main

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (RadioButton1.Checked = True) Then
            If TextBox1.Text.StartsWith("http://") Or TextBox1.Text.StartsWith("https://") Then
                If TextBox1.Text.EndsWith(".png") Or TextBox1.Text.EndsWith(".jpg") Or TextBox1.Text.EndsWith(".jpeg") Or TextBox1.Text.EndsWith(".gif") Or TextBox1.Text.EndsWith(".bmp") Then
                    MsgBox("You fail at life. Make sure you are leaving the file name out of the url.")
                Else
                    ByNumber(TextBox1.Text, CheckBox1.Checked)
                End If
            Else
                MsgBox("You fail at life. Make sure you have the whole URL.")
            End If
        End If
    End Sub

    Private Sub ByNumber(ByVal ComicPage As String, ByVal Repair As Boolean)

        My.Forms.Log.Show()

        If Repair = True Then
            My.Forms.Log.TextBox1.AppendText("(" & Time() & "): " & "Initializing Repair...")
        Else
            My.Forms.Log.TextBox1.AppendText("(" & Time() & "): " & "Initializing Download...")
        End If
        Dim FolderName As String
        Dim Start As Integer
        Dim Ending As Integer
        Dim Directory As String
        Dim Catcher As Boolean = False
        Dim Catcher2 As Boolean = False
        Dim Catcher3 As Boolean = False
        Dim Catcher4 As Boolean = False
        Dim Catcher5 As Boolean = False

        Dim I As Integer = 0

        My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & "Initialization Complete.")
        My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & "Indexing URL.")

        If TextBox1.Text.StartsWith("http://") = True Then
            Start = 8
        ElseIf TextBox1.Text.StartsWith("https://") Then
            Start = 9
        End If

        My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & "Indexing URL Complete.")
        My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & "Checking for Folders.")

        If (My.Computer.FileSystem.DirectoryExists("C:\ComicDirectory") = False) Then

            My.Forms.Log.TextBox1.AppendText(vbNewLine & Time() & "C:\ComicDirectory Does not exist! Creating Directory.")

            My.Computer.FileSystem.CreateDirectory("C:\ComicDirectory")
        End If

        My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & "Checking URL for Sub-Directory Name.")

        Ending = ComicPage.IndexOf("/", Start)
        FolderName = ComicPage.Substring(Start - 1, Ending - Start + 1)

        My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & "Sub-Directory Name: " & FolderName)

        If (My.Computer.FileSystem.DirectoryExists("C:\ComicDirectory\" & FolderName) = False) Then

            My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & "Sub-Directory: " & FolderName & " Does not Exist! Creating Directory.")

            My.Computer.FileSystem.CreateDirectory("C:\ComicDirectory\" & FolderName)
        End If
        Directory = "C:\ComicDirectory\" & FolderName

        My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & "Checking for Folders Complete.")

        My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & "Beginning Download.")

        Do
            Catcher = False
            Catcher2 = False
            Catcher3 = False
            Catcher4 = False
            Catcher5 = False

            I = I + 1

            Catcher = Query(I, Repair, ".png", ComicPage, Directory)
            Catcher2 = Query(I, Repair, ".jpeg", ComicPage, Directory)
            Catcher3 = Query(I, Repair, ".jpg", ComicPage, Directory)
            Catcher4 = Query(I, Repair, ".gif", ComicPage, Directory)
            Catcher5 = Query(I, Repair, ".bmp", ComicPage, Directory)

        Loop Until (Catcher = True And Catcher2 = True And Catcher3 = True And Catcher4 = True And Catcher5 = True)

        MsgBox("Download Complete.")

        My.Forms.Log.Hide()
    End Sub

    Public Function Query(ByVal I As Integer, ByVal Repair As Boolean, ByVal FileType As String, ByVal ComicPage As String, ByVal Directory As String) As Boolean
        Dim FileSize As Long
        Dim Request As System.Net.WebRequest
        Dim Response As System.Net.WebResponse
        Dim FileSize2 As Integer

        Try
            My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & "Querying for " & I & FileType)
            If My.Computer.FileSystem.FileExists(Directory & "\" & I & FileType) = True Then
                My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & I & FileType & " exists!")
                If Repair = True Then
                    My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & "Checking " & I & FileType & " for imperfections.")
                    FileSize = My.Computer.FileSystem.GetFileInfo(Directory & "\" & I & FileType).Length

                    Request = Net.WebRequest.Create(ComicPage & I & FileType)
                    Request.Method = Net.WebRequestMethods.Http.Get
                    Response = Request.GetResponse
                    FileSize2 = Response.ContentLength
                    If FileSize <> FileSize2 Then
                        My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & I & FileType & " is corrupted! Re-downloading.")
                        My.Computer.FileSystem.DeleteFile(Directory & "\" & I & FileType, FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)
                        My.Computer.Network.DownloadFile(ComicPage & I & FileType, Directory & "\" & I & FileType)
                    End If
                End If
            Else
                My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & I & FileType & " does not exist! Downloading.")
                My.Computer.Network.DownloadFile(ComicPage & I & FileType, Directory & "\" & I & FileType)
            End If
            Return False
        Catch ex As Exception
            My.Forms.Log.TextBox1.AppendText(vbNewLine & "(" & Time() & "): " & I & FileType & " does not exist on the website! Skipping.")
            Return True
        End Try
    End Function

    Public Function Time() As Long
        Dim _TimeSpan As TimeSpan = DirectCast((DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)), TimeSpan)
        Return CLng(_TimeSpan.TotalSeconds)
    End Function
End Class